{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.12 \u4e0d\u540c\u96c6\u5408\u4e0a\u5143\u7d20\u7684\u8fed\u4ee3\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5728\u591a\u4e2a\u5bf9\u8c61\u6267\u884c\u76f8\u540c\u7684\u64cd\u4f5c\uff0c\u4f46\u662f\u8fd9\u4e9b\u5bf9\u8c61\u5728\u4e0d\u540c\u7684\u5bb9\u5668\u4e2d\uff0c\u4f60\u5e0c\u671b\u4ee3\u7801\u5728\u4e0d\u5931\u53ef\u8bfb\u6027\u7684\u60c5\u51b5\u4e0b\u907f\u514d\u5199\u91cd\u590d\u7684\u5faa\u73af\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "itertools.chain() \u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u7b80\u5316\u8fd9\u4e2a\u4efb\u52a1\u3002\n\u5b83\u63a5\u53d7\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u6709\u6548\u7684\u5c4f\u853d\u6389\u5728\u591a\u4e2a\u5bb9\u5668\u4e2d\u8fed\u4ee3\u7ec6\u8282\u3002\n\u4e3a\u4e86\u6f14\u793a\u6e05\u695a\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from itertools import chain\na = [1, 2, 3, 4]\nb = ['x', 'y', 'z']\nfor x in chain(a, b):\nprint(x)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 chain() \u7684\u4e00\u4e2a\u5e38\u89c1\u573a\u666f\u662f\u5f53\u4f60\u60f3\u5bf9\u4e0d\u540c\u7684\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u7684\u65f6\u5019\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Various working sets of items\nactive_items = set()\ninactive_items = set()\n\n# Iterate over all items\nfor item in chain(active_items, inactive_items):\n    # Process item"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u89e3\u51b3\u65b9\u6848\u8981\u6bd4\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528\u4e24\u4e2a\u5355\u72ec\u7684\u5faa\u73af\u66f4\u52a0\u4f18\u96c5\uff0c"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for item in active_items:\n    # Process item\n    ...\n\nfor item in inactive_items:\n    # Process item\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "itertools.chain() \u63a5\u53d7\u4e00\u4e2a\u6216\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u8f93\u5165\u53c2\u6570\u3002\n\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u4f9d\u6b21\u8fde\u7eed\u7684\u8fd4\u56de\u6bcf\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u3002\n\u8fd9\u79cd\u65b9\u5f0f\u8981\u6bd4\u5148\u5c06\u5e8f\u5217\u5408\u5e76\u518d\u8fed\u4ee3\u8981\u9ad8\u6548\u7684\u591a\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Inefficent\nfor x in a + b:\n    ...\n\n# Better\nfor x in chain(a, b):\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7b2c\u4e00\u79cd\u65b9\u6848\u4e2d\uff0c a + b \u64cd\u4f5c\u4f1a\u521b\u5efa\u4e00\u4e2a\u5168\u65b0\u7684\u5e8f\u5217\u5e76\u8981\u6c42a\u548cb\u7684\u7c7b\u578b\u4e00\u81f4\u3002\nchian() \u4e0d\u4f1a\u6709\u8fd9\u4e00\u6b65\uff0c\u6240\u4ee5\u5982\u679c\u8f93\u5165\u5e8f\u5217\u975e\u5e38\u5927\u7684\u65f6\u5019\u4f1a\u5f88\u7701\u5185\u5b58\u3002\n\u5e76\u4e14\u5f53\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7c7b\u578b\u4e0d\u4e00\u6837\u7684\u65f6\u5019 chain() \u540c\u6837\u53ef\u4ee5\u5f88\u597d\u7684\u5de5\u4f5c\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}